<?php
//We've included ../Includes/FusionCharts.php and ../Includes/DBConn.php, which contains
//functions to help us easily embed the charts and connect to a database.
include("../Includes/FusionCharts.php");
include("../Includes/DBConn.php");
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>UPLB DV</title>
<script language="Javascript" src="../js/FusionCharts.js"></script>
<link href="../css/960.css" rel="stylesheet" type="text/css" />
<link href="../css/reset.css" rel="stylesheet" type="text/css" />
<link href="../css/text.css" rel="stylesheet" type="text/css" />
<link href="../css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../js/search.js"></script>

<script type="text/javascript" src="../js/jquery-1.3.1.min.js"></script>
<script type="text/javascript" src="../js/slide.js"></script>
<script type="text/javascript" src="../js/loadgallery.js"></script>
<link href="../css/slideshow.css" rel="stylesheet" type="text/css" />
</head>

<body>

	<div class="grid_8 alpha">
		<div id="gallery">		
			<?php
				//get the values before the submit button
				$snfirst = $_POST['sn_first'];
				$snlast = $_POST['sn_last'];
				$yearfirst = $_POST['year_first'];
				$yearfirst2 = $_POST['year_first2'];
				$yearlast = $_POST['year_last'];
				$yearlast2 = $_POST['year_last2'];
				$semester = $_POST['semester'];
				$semester2 = $_POST['semester2'];
				$kindofgraph = $_POST['kindofgraph'];
			
		
				echo "&nbsp;&nbsp;&nbsp;";
				echo "<form action='v_individual.php' method='post'>";
				echo "Student Number: <input type='text' name='sn_first' size='4' maxlength='4' value='$snfirst'/>-<input type='text' name='sn_last' size='5' maxlength='5' value='$snlast' />";
			
			
				echo 
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				";
		
				echo "<select name='kindofgraph'>";
				echo "<optgroup label='Single Semester'>";
				if($kindofgraph == 'FCF_COLUMN2D'){
					echo "<option value='FCF_COLUMN2D' selected='selected'>Vertical Bar Graph</option>";
				}
				else echo "<option value='FCF_COLUMN2D'>Vertical Bar Graph</option>";
				if($kindofgraph == 'FCF_BAR2D'){
					echo "<option value='FCF_BAR2D' selected='selected'>Horizontal Bar Graph</option>";
				}
				else echo "<option value='FCF_BAR2D'>Horizontal Bar Graph</option>";
				if($kindofgraph == 'FCF_LINE'){
					 echo "<option value='FCF_LINE' selected='selected'>Line Graph</option>";
				}
				else echo "<option value='FCF_LINE'>Line Graph</option>";
				echo "</optgroup>";
				
				echo "<optgroup label='Multiple Semester'>";
				if($kindofgraph == 'FCF_MSCOLUMN2D'){
					echo "<option value='FCF_MSCOLUMN2D' selected='selected'>Vertical Bar Graph</option>";
				}
				else echo "<option value='FCF_MSCOLUMN2D'>Vertical Bar Graph</option>";
				if($kindofgraph == 'FCF_MSBAR2D'){
					echo "<option value='FCF_MSBAR2D' selected='selected'>Horizontal Bar Graph</option>";
				}
				else echo "<option value='FCF_MSBAR2D'>Horizontal Bar Graph</option>";
				if($kindofgraph == 'FCF_LINE2'){
					 echo "<option value='FCF_LINE2' selected='selected'>Line Graph</option>";
				}
				else echo "<option value='FCF_LINE2'>Line Graph</option>";
				echo "</optgroup>";
				
				echo "<optgroup label='Grade Distribution'>";
				if($kindofgraph == 'FCF_PIE3D'){
					echo "<option value='FCF_PIE3D' selected='selected'>Pie Graph</option>";
				}
				else echo "<option value='FCF_PIE3D'>Pie Graph</option>";
				if($kindofgraph == 'FCF_DOUGHNUT2D'){
					echo "<option value='FCF_DOUGHNUT2D' selected='selected'>Doughnut Graph</option>";
				}
				else echo "<option value='FCF_DOUGHNUT2D'>Doughnut Graph</option>";
				if($kindofgraph == 'FCF_FUNNEL'){
					 echo "<option value='FCF_FUNNEL' selected='selected'>Funnel Graph</option>";
				}
				else echo "<option value='FCF_FUNNEL'>Funnel Graph</option>";
				echo "</optgroup>";
				
				echo "<optgroup label='GWA'>";
				if($kindofgraph == 'FCF_LINE3'){
					echo "<option value='FCF_LINE3' selected='selected'>Line Graph</option>";
				}
				else echo "<option value='FCF_LINE3'>Line Graph</option>";
				echo "</optgroup>";
				echo "</select>";
				
			
				echo "<br />";
				echo "From A.Y.: ";
				echo "<select name='semester'>";
				if($semester=='1'){
					echo "<option value='1' selected='selected'>First</option>";
				}
				else echo "<option value='1'>First</option>";
				
				if($semester=='2'){
					echo "<option value='2' selected='selected'>Second</option>";
				}
				else echo "<option value='2'>Second</option>";
				if($semester=='3'){
					echo "<option value='3' selected='selected'>Summer</option>";
				}
				else echo "<option value='3'>Summer</option>";
				echo "</select>";
				echo "<input type='text' name='year_first' size='4' maxlength='4' value='$yearfirst' />-<input type='text' name='year_last' size='4' maxlength='4' value='$yearlast' />";
				
				echo " to A.Y.: ";
				echo "<select name='semester2'>";
				if($semester2=='1'){
					echo "<option value='1' selected='selected'>First</option>";
				}
				else echo "<option value='1'>First</option>";
				
				if($semester2=='2'){
					echo "<option value='2' selected='selected'>Second</option>";
				}
				else echo "<option value='2'>Second</option>";
				if($semester2=='3'){
					echo "<option value='3' selected='selected'>Summer</option>";
				}
				else echo "<option value='3'>Summer</option>";
				echo "</select>";
				echo " <input type='text' name='year_first2' size='4' maxlength='4' value='$yearfirst2' />-<input type='text' name='year_last2' size='4' maxlength='4' value='$yearlast2' />";
				echo "<input type='submit' />";
				echo "</form>";
			
				
				$snnumber = $snfirst.$snlast;
				$studentnumber = $snfirst."-".$snlast;
				$academicyear = $yearfirst.$yearlast;
				$academicyear2 = $yearfirst2.$yearlast2;
				$ayear = $yearfirst."-".$yearlast;
				
				
				// Connect to the DB
				$link = connectToDB();

				//$strXML will be used to store the entire XML document generated
				//Generate the graph element
				//fetch the name of the specified student number
				$strQuery0 = "select * from student where studentNumber='$snnumber'";
				$result0 = mysql_query($strQuery0) or die(mysql_error());
				$ors = mysql_fetch_array($result0);
				$fullname = $ors['firstName']." ".$ors['lastName'];
				
				if($semester == 1){
					$semname ="First";
				}
				else if($semester == 2){
					$semname = "Second";
				}
				else if($semester == 3){
					$semname = "Third";
				}
				$schoolyear = $semname." Semester ".$ayear;
							
				if($semester2 == 1){
					$semname2 ="First";
				}
				else if($semester2 == 2){
					$semname2 = "Second";
				}
				else if($semester2 == 3){
					$semname2 = "Third";
				}


				//if the selected graphs is for single semester
				if(($kindofgraph=='FCF_COLUMN2D') || ($kindofgraph=='FCF_BAR2D') || ($kindofgraph=='FCF_LINE')){
					$strXML = "<graph caption='$fullname' subCaption='$schoolyear' showBorder='1' showNames='1' formatNumberScale='0' decimalPrecision='2' yaxisminvalue='0' yaxismaxvalue='9' numdivlines='35'> ";
					
					// Fetch all factory records
					$strQuery = "select * from subjects where studentNumber='$snnumber' and academicYear='$yearfirst' and semester='$semester'";
					$result = mysql_query($strQuery) or die(mysql_error());
							
					//Iterate through each factory
					if ($result) {
						while($ors = mysql_fetch_array($result)) {
							$strXML .= "<set name='" . $ors['courseCode'] . "' value='" . $ors['grade'] . "' />";
						}
					}
				
					//close the link
					mysql_close($link);

					//Finally, close <graph> element
					$strXML .= "</graph>";
					$graph = $kindofgraph.".swf";
					//Create the chart - Pie 3D Chart with data from $strXML
					echo renderChart("../FusionCharts/$graph", "", $strXML, "FactorySum", 980, 450);
				}
				
				else if(($kindofgraph=='FCF_PIE3D') || ($kindofgraph=='FCF_DOUGHNUT2D') || ($kindofgraph=='FCF_FUNNEL') ){
					$strXML = "<graph caption='$fullname' subCaption='$schoolyear' pieSliceDepth='30' showBorder='1' showNames='1' numberSuffix='%'  formatNumberScale='0' decimalPrecision='2'> ";
				
					// Fetch all factory records
					$strQuerytotal = "select COUNT(grade) as totalsubjects from subjects where studentNumber='$snnumber' and academicYear='$yearfirst' and semester='$semester'";
					$resulttotal = mysql_query($strQuerytotal) or die(mysql_error());
					$orstotal = mysql_fetch_array($resulttotal);
					
					$strQuery = "select grade, COUNT(grade) as counter from subjects where studentNumber='$snnumber' and academicYear='$yearfirst' and semester='$semester' GROUP BY grade";
					$result = mysql_query($strQuery) or die(mysql_error());
							
					//Iterate through each factory
					if ($result) {
						while($ors = mysql_fetch_array($result)) {
							$percentage = ($ors['counter']/$orstotal['totalsubjects'])*100;
							//echo $ors['grade']." ".$ors['counter']." ".$percentage;
							//echo "<br />";
							$strXML .= "<set name='" . $ors['grade'] . "' value='" . $percentage . "' />";
						}
					}
				
					//close the link
					mysql_close($link);

					//Finally, close <graph> element
					$strXML .= "</graph>";
					$graph = $kindofgraph.".swf";
					//Create the chart - Pie 3D Chart with data from $strXML
					echo renderChart("../FusionCharts/$graph", "", $strXML, "FactorySum", 980, 450);
				
				}				
				//line graph for GWA
				else if($kindofgraph=='FCF_LINE3'){
					$kindofgraph = "FCF_LINE";
					$strXML = "<graph caption='$fullname' subCaption='$schoolyear' pieSliceDepth='30' showBorder='1' showNames='1'  formatNumberScale='0' decimalPrecision='4' yaxisminvalue='0' yaxismaxvalue='5' numdivlines='19'> ";
					$ay=$yearfirst;
					
					while($ay >=$yearfirst && $ay<=$yearfirst2){
						if($ay==$yearfirst2 && $semester > $semester2){
							//echo $semester2;
							break;
						}
						else{
							// Fetch all factory records
							$strQuerytotal = "select SUM(units) as totalunits from subjects where studentNumber='$snnumber' and academicYear='$ay' and semester='$semester'";
							$resulttotal = mysql_query($strQuerytotal) or die(mysql_error());
							$orstotal = mysql_fetch_array($resulttotal);
							
							
							if($orstotal['totalunits']>0){
								$strQuery = "select * from subjects where studentNumber='$snnumber' and academicYear='$ay' and semester='$semester'";
								$result = mysql_query($strQuery) or die(mysql_error());
							
								$total = 0;
							
								//Iterate through each factory
								if ($result) {
									while($ors = mysql_fetch_array($result)) {
									
										$total = $total + ($ors['grade'] * $ors['units']);
										//echo $total;
										//$strXML .= "<set name='" . $ors['grade'] . "' value='" . $percentage . "' />";
									}
								}
								$gwa = $total/$orstotal['totalunits'];
								//echo $gwa;
								$strXML .= "<set name='" . $semname." Semester ".$academicyear. "' value='" . $gwa . "' />";
							}//end of outside if
							if($semester == 3){
								$ay=$ay + 1;
								$academicyear = $academicyear + 10001;
								$semester=1;
							}
							else{
								$semester++;
							}
						}
					}//end of while
					
					//close the link
					mysql_close($link);

					//Finally, close <graph> element
					$strXML .= "</graph>";
					$graph = $kindofgraph.".swf";
					//Create the chart - Pie 3D Chart with data from $strXML
					echo renderChart("../FusionCharts/$graph", "", $strXML, "FactorySum", 980, 500);
				}
				else{
					//echo "hay";
					$strXML = "<graph caption='$fullname' subCaption='$schoolyear' showBorder='1' showNames='1' formatNumberScale='0' decimalPrecision='2' yaxisminvalue='0' yaxismaxvalue='5' numdivlines='19'> ";
					$strCategories = "<categories>";
					// Fetch all factory records
					$strQuery = "select * from subjects where studentNumber='$snnumber' and academicYear='$yearfirst' and semester='$semester'";
					$result = mysql_query($strQuery) or die(mysql_error());
							
					$strDataSet = "<dataset color='AFD8F8' >";
					//Iterate through each factory
					if ($result) {
						while($ors = mysql_fetch_array($result)) {
							$strCategories .= "<category name='" . $ors['courseCode']. "' />";
							echo $ors['grade'];
							$strDataSet .= "<set name='" . $ors['courseCode'] . "' value='" . $ors['grade'] . "' />";
						}
					}
					$strCategories .= "</categories>";
					$strDataSet .= "</dataset>";
					//close the link
					mysql_close($link);

					//Finally, close <graph> element
					$strXML .= $strCategories.$strDataSet."</graph>";
					$graph = $kindofgraph.".swf";
					//Create the chart - Pie 3D Chart with data from $strXML
					echo renderChart("../FusionCharts/$graph", "", $strXML, "FactorySum", 980, 450);
				}
				
			?>
		</div>	
	</div>


</body>
</html>
